using HappyDog.Services;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.Collections.Generic;

namespace HappyDog.Pages
{
    public class IndexModel : PageModel
    {
        private readonly ILogger<IndexModel> _logger;
        private readonly IConfiguration config;
        public List<m_Dog> dogs { get; set; }

        public IndexModel(ILogger<IndexModel> logger,IConfiguration config)
        {
            _logger = logger;
            this.config = config;
        }

        public void OnGet()
        {
            var dogsconfig = config.GetSection("dogs");
            List<m_Dog> ds = new List<m_Dog>();
            foreach (var dog in dogsconfig.GetChildren())
            {
                var now = DateTime.Now;
                var dog_name = dog.GetValue<string>("dog_name");
                var food_file_path = dog.GetValue<string>("food_file_path");
                var hunger_endurance_second = dog.GetValue<int>("hunger_endurance_second");
                var hunger_count = dog.GetValue<int>("hunger_count");
                var script_after_die = dog.GetSection("script_after_die");
                var script_command = script_after_die.GetValue<string>("command");
                var script_arguments = script_after_die.GetValue<string>("arguments");
                var script_workdir = script_after_die.GetValue<string>("script_workdir");
                var script_verb = script_after_die.GetValue<string>("verb");
                var d = new m_Dog()
                {
                    dog_name = dog_name,
                    hunger_endurance_second = hunger_endurance_second,
                    food_file_path = food_file_path,
                    hunger_count = hunger_count,
                    script_after_die = new m_Script_After_Die()
                    {
                        command = script_command,
                        verb = script_verb,
                        arguments=script_arguments,
                        workdir= script_workdir
                    }
                };

                d.lastsending = TempStore.Instance.lastsendingtime.GetDicValue(dog_name);
                d.lastcrytime = TempStore.Instance.lastcrytime.GetDicValue(dog_name);
                d.lasteattime = TempStore.Instance.lasteattime.GetDicValue(dog_name);
                d.lasthungertime = TempStore.Instance.lasthungertime.GetDicValue(dog_name);

                ds.Add(d) ;

            }
            dogs = ds;

        }
    }

    public class m_Dog
    {
        public string dog_name { get; set; }
        public string food_file_path { get; set; }
        public int hunger_endurance_second { get; set; }
        public int hunger_count { get; set; }
        public m_Script_After_Die script_after_die { get; set; }
        public string lastsending { get; set; }
        public string lasteattime { get; set; }
        public string lastcrytime { get; set; }
        public string lasthungertime { get; set; }
    }

    public class m_Script_After_Die
    {
        public string command { get; set; }
        public string arguments { get; set; }
        public string workdir { get; set; }
        public string verb { get; set; }
    }

}
